package org.galaxyworld.flycode.gui;

import org.galaxyworld.flycode.config.AppConfigData;
import org.galaxyworld.flycode.config.ConfigManager;
import org.galaxyworld.flycode.events.MainWindowEvent;
import org.galaxyworld.flycode.events.MainWindowEventListener;

import javax.swing.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class AppConfigPane extends AbstractConfigPane implements MainWindowEventListener {

    private JTextField outputDirField;

    private boolean shown = false;

    public AppConfigPane() {
        super("Application");

        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent e) {
                shown = true;
            }

            @Override
            public void componentHidden(ComponentEvent e) {
                if (shown) {
                    AppConfigData appConfigData = ConfigManager.getConfig(AppConfigData.class);
                    appConfigData.setOutputDirPath(outputDirField.getText());
                }
            }
        });
    }

    @Override
    protected void setupMainPane(JPanel mainPane) {
        GroupLayout layout = new GroupLayout(mainPane);
        mainPane.setLayout(layout);

        JLabel outputDirLabel = new JLabel("Output Directory");
        outputDirField = new JTextField();

        layout.setHorizontalGroup(
                layout.createSequentialGroup()
                        .addGap(GAP)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addComponent(outputDirLabel))
                        .addGap(MARGIN)
                        .addGroup(layout.createParallelGroup()
                                .addComponent(outputDirField))
                        .addGap(GAP));
        layout.setVerticalGroup(
                layout.createSequentialGroup()
                        .addGap(GAP)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(outputDirLabel)
                                .addComponent(outputDirField))
                        .addGap(GAP));
    }

    @Override
    public void windowOpened(MainWindowEvent e) {
        AppConfigData appConfigData = ConfigManager.getConfig(AppConfigData.class);
        outputDirField.setText(appConfigData.getOutputDirPath());
    }

    @Override
    public void windowClosing(MainWindowEvent e) {

    }

}
